package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加商品到购物车
     * @param shoppingCartDTO 商品信息
     *  业务分析:
     *      1.先判断要添加的商品在购物车中是否存在.
     *      2.存在就做修改操作,数量+1
     *      3.不存在就做添加操作
     *      4.判断是添加菜品还是套餐,查询对应信息,补满信息添加到购物车中
     *
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //先查询购物车中是否存在该商品
        ShoppingCart dbShoppingCart = shoppingCartMapper.findByConditions(shoppingCart);
        if (dbShoppingCart != null){
            //做修改操作,数量+1
            dbShoppingCart.setNumber(dbShoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumber(dbShoppingCart);
        }else {
            //通过是否有dishId判断添加的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            //添加菜品
            if (dishId != null){
                Dish dish = dishMapper.findById(dishId);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                //否则添加套餐
            }else {
                Setmeal setmeal = setmealMapper.findById(shoppingCartDTO.getSetmealId());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
            }
            //设置公共字段
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            //做添加操作
            shoppingCartMapper.add(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.findByUserId(userId);
    }

    /**
     * 购物车中商品数量减1
     * @param shoppingCartDTO 商品信息
     *  业务分析:
     *    1.先根据数据查询商品的数量
     *    2.如果商品数量>1,做修改操作(数量-1)
     *    3.如果商品数量=1,直接删除这条商品
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //根据数据线查询商品的数量
        ShoppingCart dbShoppingCart = shoppingCartMapper.findByConditions(shoppingCart);
        //如果数量>1,做修改操作(数量-1)
        if (dbShoppingCart.getNumber() > 1){
            dbShoppingCart.setNumber(dbShoppingCart.getNumber() - 1);
            shoppingCartMapper.updateNumber(dbShoppingCart);
            //如果数量=1,直接删除这个商品
        }else {
            shoppingCartMapper.delById(dbShoppingCart.getId());
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.delByUserId(userId);
    }
}
